/**
 * 详情对话框
 */
var InfoInfoDlg = {
    data: {
        goodsId: "",
        goodsCode: "",
        classification: "",
        type: "",
        brand: "",
        model: "",
        company: "",
        count: "",
        remarks: "",
        frontPhoto: "",
        backPhoto: "",
        version: "",
        createTime: "",
        updateTime: "",
        createUser: "",
        updateUser: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;

    //动态添加单位
    var addcompany = function() {
        let aj=new $ax(Feng.ctxPath + "/company/getAll", function (data) {
            $.each(data, function (index, item) {
                $('#companyRadio').append('<input type="radio" name="company" title='+item.name+' value='+item.companyId+'>');
            });
            layui.form.render("radio");
        });
        aj.start();
    }
    addcompany();

    //动态添加类型
    var addtype = function(){
        let aj=new $ax(Feng.ctxPath + "/type/getAll?classification=2", function (data) {
            $('#type').empty();//清空下拉框的值
            $.each(data, function (index, item) {
                $('#type').append(new Option(item.typeName, item.typeId));// 下拉菜单里添加元素
            });
            layui.form.render("select");
        });
        aj.start();
    }
    addtype();

    //普通图片上传
    upload.render({
        elem: '#picBtn'
        , url: Feng.ctxPath + '/system/upload'
        ,accept: 'images'
        ,exts: 'png|jpeg'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#img1').attr('src', result);
            });
        }
        , done: function (res) {
            $("#frontPhoto").val(res.data.fileId);
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传图片失败！");
        }
    });

    upload.render({
        elem: '#picBtn2'
        , url: Feng.ctxPath + '/system/upload'
        ,accept: 'images'
        ,exts: 'png|jpeg'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#img2').attr('src', result);
            });
        }
        , done: function (res) {
            $("#backPhoto").val(res.data.fileId);
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传图片失败！");
        }
    });

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/info/detail?goodsId=" + Feng.getUrlParam("goodsId"),function (data) {
        var aj=new $ax(Feng.ctxPath + "/file/get?id="+data.data.frontPhoto, function (da) {
            if(da.response != false) {
                $('#img1').attr("src", da.data);
            }
        });
        aj.start();
        var aj1=new $ax(Feng.ctxPath + "/file/get?id="+data.data.backPhoto, function (da) {
            if(da.response != false) {
                $('#img2').attr("src", da.data);
            }
        });
        aj1.start();
    });
    var result = ajax.start();
    form.val('infoForm', result.data);


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/info/editItem", function (data) {
            Feng.success("更新成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

});